﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyWeb.ServiceBus.Gateway.Model
{
    public class FileRoute
    {
        public FileRoute()
        {
            AddClaimsToRequest = new Dictionary<string, string>();
            AddHeadersToRequest = new Dictionary<string, string>();
            AddQueriesToRequest = new Dictionary<string, string>();
            ChangeDownstreamPathTemplate = new Dictionary<string, string>();
            DownstreamHeaderTransform = new Dictionary<string, string>();
            DownstreamHostAndPorts = new List<FileHostAndPort>();
            Priority = 0;
            UpstreamHeaderTransform = new Dictionary<string, string>();
            UpstreamHttpMethod = new List<string>();
        }

        public FileRoute(FileRoute from)
        {
            DeepCopy(from, this);
        }
        /// <summary>
        /// 将Claims添加到请求
        /// </summary>
        public Dictionary<string, string> AddClaimsToRequest { get; set; }
        /// <summary>
        /// 将Header添加到请求
        /// </summary>
        public Dictionary<string, string> AddHeadersToRequest { get; set; }
        /// <summary>
        /// 将Queries添加到请求
        /// </summary>
        public Dictionary<string, string> AddQueriesToRequest { get; set; }
        /// <summary>
        /// 下游路径模板
        /// </summary>
        public Dictionary<string, string> ChangeDownstreamPathTemplate { get; set; }
        /// <summary>
        /// 下游Header转换
        /// </summary>
        public Dictionary<string, string> DownstreamHeaderTransform { get; set; }
        /// <summary>
        /// 下游主机和端口
        /// </summary>
        public List<FileHostAndPort> DownstreamHostAndPorts { get; set; }
        /// <summary>
        /// 下游请求方法
        /// </summary>
        public string DownstreamHttpMethod { get; set; }
        /// <summary>
        /// 下游http版本
        /// </summary>
        public string DownstreamHttpVersion { get; set; }
        /// <summary>
        /// 下游路径模板
        /// </summary>
        public string DownstreamPathTemplate { get; set; }
        /// <summary>
        /// 下游方案
        /// </summary>
        /// <remarks>http/https</remarks>
        public string DownstreamScheme { get; set; }
        /// <summary>
        /// 负载均衡的算法
        /// </summary>
        public string LoadBalancerOptions { get; set; }
        /// <summary>
        /// 优先事项
        /// </summary>
        /// <remarks>匹配优先级 默认所有配置的路由优先级的值为0，值越大，优先级越高</remarks>
        public int Priority { get; set; }
        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; set; }
        /// <summary>
        /// 服务命名空间
        /// </summary>
        public string ServiceNamespace { get; set; }
        /// <summary>
        /// 超时时间
        /// </summary>
        public int Timeout { get; set; }
        /// <summary>
        /// 上游Header转换
        /// </summary>
        public Dictionary<string, string> UpstreamHeaderTransform { get; set; }
        public string UpstreamHost { get; set; }
        /// <summary>
        /// 上游Http方法
        /// </summary>
        public List<string> UpstreamHttpMethod { get; set; }
        /// <summary>
        /// 上游路径模板
        /// </summary>
        public string UpstreamPathTemplate { get; set; }

        /// <summary>
        /// 通过进行深度复制来克隆此对象。
        /// </summary>
        /// <returns>A <see cref="FileRoute"/> 深度复制对象。</returns>
        public object Clone()
        {
            var other = (FileRoute)MemberwiseClone();
            DeepCopy(this, other);
            return other;
        }

        public static void DeepCopy(FileRoute from, FileRoute to)
        {
            to.AddClaimsToRequest = new(from.AddClaimsToRequest);
            to.AddHeadersToRequest = new(from.AddHeadersToRequest);
            to.AddQueriesToRequest = new(from.AddQueriesToRequest);
            to.ChangeDownstreamPathTemplate = new(from.ChangeDownstreamPathTemplate);
            to.DownstreamHeaderTransform = new(from.DownstreamHeaderTransform);
            to.DownstreamHostAndPorts = from.DownstreamHostAndPorts.Select(x => new FileHostAndPort(x)).ToList();
            to.DownstreamHttpMethod = from.DownstreamHttpMethod;
            to.DownstreamHttpVersion = from.DownstreamHttpVersion;
            to.DownstreamPathTemplate = from.DownstreamPathTemplate;
            to.DownstreamScheme = from.DownstreamScheme;
            to.LoadBalancerOptions = new(from.LoadBalancerOptions);
            to.Priority = from.Priority;
            to.ServiceName = from.ServiceName;
            to.ServiceNamespace = from.ServiceNamespace;
            to.Timeout = from.Timeout;
            to.UpstreamHeaderTransform = new(from.UpstreamHeaderTransform);
            to.UpstreamHost = from.UpstreamHost;
            to.UpstreamHttpMethod = new(from.UpstreamHttpMethod);
            to.UpstreamPathTemplate = from.UpstreamPathTemplate;
        }
    }
}
